﻿using System;
using UnityEngine;

namespace GameFramework.common
{
    public class MonoSingleton<T>:MonoBehaviour where T:MonoBehaviour
    {
        private static T _instance;

        protected MonoSingleton()
        {
            init();
        }

        private void Awake()
        {
            if (_instance != null)
            {
                Destroy(_instance);
            }
            
            _instance = this as T;
        }

        public static T Instance
        {
            get
            {
                if (_instance == null)
                {
                    throw new Exception("未被赋值就尝试获取实例：" + typeof(T).Name);
                }

                return _instance;
            }
        }

        protected virtual void init()
        {
        }

    }
    
    
    
}